import { _decorator, Component, Node, Prefab, Size } from 'cc';
import { GameApp } from 'db://assets/scripts/game';
import Biz from './scripts/biz';
import { EventName } from './scripts/consts/events';
import { addBackgroundEntity } from './scripts/game/entity/background';
import { UiPlayGame } from './scripts/ui/game';
import { Keyboard } from './scripts/ui/keyboard';
import { UiSettlement } from './scripts/ui/settlement';
import { UiStart } from './scripts/ui/start';
import Utils from './scripts/utils';
const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {
    @property({ displayName: '速度' })
    speed: number = 100;
    @property({ type: [Prefab], displayName: '敌人' })
    enemies: Prefab[] = [];
    @property({ type: [Prefab], displayName: '子弹' })
    bullets: Prefab[] = [];

    private uiPlayGame: UiPlayGame;
    private uiSettlement: UiSettlement;
    private uiStart: UiStart;
    private runtime: GameApp;
    private bg: Node;
    protected onLoad(): void {
        // 全局变量
        Biz.global.rootGame = new Node('game');
        this.node.addChild(Biz.global.rootGame);
        Biz.global.rootEnemy = new Node('enemy');
        Biz.global.rootGame.addChild(Biz.global.rootEnemy);
        Biz.global.rootBullet = new Node('bullet');
        Biz.global.rootGame.addChild(Biz.global.rootBullet);

        // ui
        this.uiPlayGame = this.node.getChildByPath('ui/game').addComponent(UiPlayGame);
        this.uiSettlement = this.node.getChildByPath('ui/settlement').addComponent(UiSettlement);
        this.uiStart = this.node.getChildByPath('ui/start').addComponent(UiStart);
        this.uiStart.show();

        // 背景
        this.bg = this.node.getChildByPath('bg');
        addBackgroundEntity(this.node.getChildByPath('bg/bg1'), this.speed);
        addBackgroundEntity(this.node.getChildByPath('bg/bg2'), this.speed);
        addBackgroundEntity(this.node.getChildByPath('bg/bg3'), this.speed);

        // 游戏运行时
        this.runtime = this.addComponent(GameApp);
        this.runtime.init(this.enemies, this.bullets);

        // 中控事件
        Utils.events.on(EventName.StartGame, this.startGame, this);
        Utils.events.on(EventName.PauseGame, this.pauseGame, this);
        Utils.events.on(EventName.ResumeGame, this.resumeGame, this);
        Utils.events.on(EventName.EndGame, this.endGame, this);

        // 键盘事件
        this.addComponent(Keyboard);

        const clipTest = new Node('clip');
        this.node.addChild(clipTest);
        Utils.animation.addAnimationClip(clipTest, 'sprites/player/down', { loop: true, name: 'playerDown', size: new Size(50, 50) });
    }

    protected onDestroy(): void {
        Utils.events.off(EventName.StartGame, this.startGame, this);
        Utils.events.off(EventName.PauseGame, this.pauseGame, this);
        Utils.events.off(EventName.ResumeGame, this.resumeGame, this);
        Utils.events.off(EventName.EndGame, this.endGame, this);
    }

    // ---------------------游戏中控--------------------

    // 开始游戏
    private startGame() {
        this.bg.active = true;
        this.uiStart.hide();
        this.uiSettlement.hide();
        this.uiPlayGame.show();
        this.uiPlayGame.setPlay(true);
        Biz.global.isStart = true;
        Biz.score.resetScore();
        Biz.hp.resetHp(6);
    }

    // 暂停游戏
    private pauseGame() {
        this.uiPlayGame.setPlay(false);
        Biz.global.isStart = false;
    }

    // 继续游戏
    private resumeGame() {
        this.uiPlayGame.setPlay(true);
        Biz.global.isStart = true;
    }

    // 结束游戏
    private endGame() {
        this.bg.active = false;
        this.uiPlayGame.hide();
        this.uiStart.hide();
        this.uiSettlement.setScore();
        this.uiSettlement.show();
        Biz.global.isStart = false;
    }

    // #endregion
}
